<?php
/**
 * @version SVN: $Id: translator.php 1216 2010-05-28 00:02:46Z elkuku $
 * @package    EasyCreator
 * @subpackage Views
 * @author     EasyJoomla {@link http://www.easy-joomla.org Easy-Joomla.org}
 * @author     Nikolai Plath (elkuku) {@link http://www.nik-it.de NiK-IT.de}
 * @author     Created on 11-Sep-2008
 * @license    GNU/GPL, see JROOT/LICENSE.php
 */

//-- No direct access
defined ('_JEXEC') or die ('=;)');

//--Add CSS
ecrStylesheet('icon');
ecrStylesheet('translator');

ecrScript('php2js');

$useGoogle = JComponentHelper::getParams('com_easycreator')->get('use_google_trans_api');

$fieldID = JRequest::getInt('field_id');

$baseLink = 'index.php?option=com_easycreator&tmpl=component&controller=ajax&format=raw';
$baseLink .= '&ecr_project='.JRequest::getCmd('ecr_project');
$baseLink .= '&scope='.$this->scope;
$baseLink .= '&trans_lang='.$this->trans_lang;
$baseLink .= '&trans_key='.$this->trans_key;

$ret_type = JRequest::getCmd('ret_type', 'ini');

if($this->trans_lang != 'en-GB' && ! $this->trans_default)
{
    //-- No default translation available
	ecrHTML::displayMessage(array(JText::_('Unable to translate without default translation'), JText::sprintf('Please translate %s first', 'en-GB')), 'error');

	return;
}

$translation = '';

if($this->translation)
{
	$translation =($this->translation == '**TRANSLATE**') ? '' : $this->translation;
}
elseif($this->trans_lang == 'en-GB')
{
    $translation = ucfirst(strtolower($this->trans_key));
    $translation = str_replace('_', ' ', $translation);
}

if(strpos($translation, '"') === 0)
{
    $translation = substr($translation, 1);
}

if(strrpos($translation, '"') == strlen($translation) - 1)
{
    $translation = substr($translation, 0, strlen($translation) - 1);
}
?>

<script type="text/javascript">
function copyTrans()
{
    var s = stripQuotes(trim($('default').innerHTML));
	$('translation').value = s;
	$('translation').focus();
}//function

function stripQuotes(s)
{
    if(s.substr(0, 1) == '"')
    {
        s = s.substr(1);
    }

    if(s.substr(s.length - 1) == '"')
    {
        s = s.substr(0, s.length - 1);
    }

    return s;
}

function ajTranslate()
{
	new Ajax('<?php echo $baseLink.'&task=translate'; ?>',
	{
		method:'post',
		data: 'translation='+encodeURIComponent($('translation').value),

		onComplete: function(response)
		{
            var resp = Json.evaluate(response);

            if( ! resp.status)
            {
                $('ajResult').innerHTML = '<strong style="color: red;">'+resp.text+'</strong>';

                return false;
                //-- Error
            }

			doc = window.parent.document;

			<?php
			switch($ret_type)
			{
				case 'ini':
					echo "doc.getElementById('trfield_".$fieldID."').innerHTML=$('translation').value;\n";
					echo "doc.getElementById('sbox-window').close();\n";
				break;

				case 'phpxml':
					echo "doc.getElementById('trfield_".$fieldID."').innerHTML='".$this->trans_lang."';\n";
					echo "doc.getElementById('trfield_".$fieldID."').style.color='green';\n";

					$adIDs = JRequest::getVar('ad_ids');

					if($adIDs)
					{
						$adIDs = explode(',', $adIDs);

						foreach ($adIDs as $adID)
						{
							echo "doc.getElementById('trfield_".$adID."').style.display='inline';\n";
						}//foreach
					}

					switch(ECR_JVERSION)
					{
						case '1.5':
        					echo "doc.getElementById('sbox-window').close();\n";
						break;

						case '1.6':
        					echo 'parent.SqueezeBox.close()';
						break;

						default:
							JError::raiseWarning(100, 'undefined JVersion');
						break;
					}//switch
				break;

				default:
					ecrHTML::displayMessage('undefined ret type', 'notice');
				break;
			}//switch
			?>
		}
	}).request();
}//function

function deleteTranslation()
{
	$('translation').value = 'Deleting...';

	new Ajax('<?php echo $baseLink.'&task=delete_translation'; ?>',
	{
		method:'post',

		onComplete: function(response)
		{
            var resp = Json.evaluate(response);

            if( ! resp.status)
            {
                //-- Error
                $('ajResult').innerHTML = '<strong style="color: red;">'+resp.text+'</strong>';

                return false;
            }

			doc = window.parent.document;
			<?php
			$html = '<strong style="color: red">'.JText::_('EMPTY').'</strong>';
			echo "doc.getElementById('trfield_".$fieldID."').innerHTML='".$html."';\n";
			switch(ECR_JVERSION)
			{
				case '1.5':
					echo "doc.getElementById('sbox-window').close();\n";
				break;

				case '1.6':
					echo 'parent.SqueezeBox.close()';
				break;

				default:
					JError::raiseWarning(100, 'undefined JVersion');
				break;
			}//switch
			?>
		}
	}).request();
}//function
</script>
<?php

if($useGoogle &&  $this->trans_lang != 'en-GB') :
	echo '<div id="google_loader">';
	echo '<img src="'.JURI::root().'administrator/components/com_easycreator/assets/images/ajax-loader2.gif" title="loader gif" />';
	echo JText::_('Loading Google Translation API');
	echo '</div>';
	?>
	<script type="text/javascript" src="http://www.google.com/jsapi"></script>
	<script type="text/javascript">
		google.load('language', '1');
		var gbranding_displayed = false;
		$('google_loader').innerHTML = '';

		function google_translate()
		{
			$('translation').value = JSLNG_TRANSLATING;

			if( ! gbranding_displayed)
			{
				google.language.getBranding('gtranslate_branding');
				gbranding_displayed = true;
			}

			var trans_lang = '<?php echo substr($this->trans_lang, 0, 2); ?>';
			var text = stripQuotes(trim($('default').innerHTML));

			google.language.translate(text, 'en', trans_lang, function(result)
			{
				if( ! result.error)
				{
					$('translation').value = result.translation;
				}
			});
		}//function
	</script>
	<?php
endif;
?>

<table width="100%">
	<tr>
		<th style="background-color: #CCE5FF;"><?php echo JText::_('Key'); ?></th>
		<td style="background-color: #CCE5FF;"><?php echo htmlentities($this->trans_key); ?></td>
	</tr>
	<?php if($this->trans_lang != 'en-GB') : ?>
	<tr>
		<th style="background-color: #FFFF99;">en-GB</th>
		<td>
		<div style="background-color: #FFFF99;" id="default"><?php echo htmlentities($this->trans_default); ?>
		</div>
		</td>
	</tr>
	<?php endif; ?>
		<tr>
		<th style="background-color: #E5FF99;"><?php echo $this->trans_lang; ?></th>
		<td style="background-color: #E5FF99;">
		<textarea name="translation" id="translation" rows="4"
			style="width: 100%; font-size: 1.3em;"><?php echo htmlspecialchars($translation); ?></textarea>
		</td>
	</tr>
</table>

<div class="ecr_easy_toolbar" style="float: right;">
    <ul>
        <li>
            <a href="javascript:;" onclick="ajTranslate();">
                <span class="icon-32-ecr_save"></span>
                <?php echo JText::_('Translate'); ?>
            </a>
        </li>
        <?php if($translation) : ?>
        <li>
            <a href="javascript:;" onclick="deleteTranslation();">
            	<span class="icon-32-ecr_delete"></span>
            	<?php echo JText::_('Delete'); ?>
            </a>
        </li>
        <?php endif; ?>
    </ul>
</div>

<?php if($this->trans_lang != 'en-GB') : ?>
<div class="ecr_easy_toolbar" style="float: left;">
    <ul>
        <li>
            <a href="javascript:;" onclick="copyTrans();">
            	<span class="icon-16-copytrans"></span>
            	<?php echo JText::_('Copy'); ?>
            </a>
        </li>
    	<?php if($useGoogle) : ?>
            <li>
            	<span id="gtranslate_branding" style="float: right; padding-left: 0.5em;"></span>
                <a href="javascript:;" onclick="google_translate();">
                	<span class="icon-16-copytrans"></span>
                	<?php echo JText::_('Google translate'); ?>
                </a>
        	</li>
    	<?php endif; ?>
    </ul>
</div>
<?php endif; ?>

<script type="text/javascript">
	$('translation').focus();
</script>
